﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web.Mvc;
using Nop.Admin;
using Nop.Services.Blogs;
using SDF.Core;
using SDFAuth.Models;

namespace SDFAuth.Web.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Account/
        
        /*private readonly IApplicationService _ApplicationService;
        public LoginController(IApplicationService ApplicationService)
        {
            _ApplicationService = ApplicationService;
        }*/

        public IUserService _UserService { get; set; }
        
        public ActionResult Login()
        {
            HtmlHelper.ClientValidationEnabled = false;
            HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
            //TODO 实现自定义客户端验证
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            var entity = model.ToEntity();
            var exist =_UserService.Exist(entity);
            if (!exist)
            {
                ModelState.AddModelError("NotExist", "用户名或者密码出错。");
            }
            return View(model);
        }

        [HttpGet] // 只能用GET ！！！
        public ActionResult Exists(string username)
        {
            var exists = _UserService.ExistUser(username);
            return Json(!exists, JsonRequestBehavior.AllowGet);
        }
      
        public FileContentResult ValidateCode()
        {
            HttpContext.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); //不缓存

            // 在此处放置用户代码以初始化页面
            string vnum;
            vnum = GetByRndNum(4);
            Session[Consts.LoginValidateCode] = vnum;
            return File(CreateValidateCode(vnum), @"image/jpeg");
        }

        private byte[] CreateValidateCode(string vnum)
        {
            Bitmap Img = null;
            Graphics g = null;
            Random random = new Random();
            int gheight = vnum.Length * 15;
            Img = new Bitmap(gheight, 24);
            g = Graphics.FromImage(Img);
            Font f = new Font("宋体", 14, FontStyle.Bold);
            //Font f = new Font("宋体", 9, FontStyle.Bold);

            g.Clear(Color.White);//设定背景色
            Pen blackPen = new Pen(ColorTranslator.FromHtml("#BCBCBC"), 3);

            for (int i = 0; i < 128; i++)// 随机产生干扰线，使图象中的认证码不易被其它程序探测到
            {
                int x = random.Next(gheight);
                int y = random.Next(20);
                int xl = random.Next(6);
                int yl = random.Next(2);
                g.DrawLine(blackPen, x, y, x + xl, y + yl);
            }

            SolidBrush s = new SolidBrush(ColorTranslator.FromHtml("#411464"));
            g.DrawString(vnum, f, s, 3, 3);

            //画边框
            blackPen.Width = 1;
            g.DrawRectangle(blackPen, 0, 0, Img.Width - 1, Img.Height - 1);
            MemoryStream ms = new MemoryStream();
            Img.Save(ms, ImageFormat.Jpeg);
            s.Dispose();
            f.Dispose();
            blackPen.Dispose();
            g.Dispose();
            Img.Dispose();

            return ms.ToArray();
        }

        //-----------------给定范围获得随机颜色
        Color GetByRandColor(int fc, int bc)
        {
            Random random = new Random();

            if (fc > 255)
                fc = 255;
            if (bc > 255)
                bc = 255;
            int r = fc + random.Next(bc - fc);
            int g = fc + random.Next(bc - fc);
            int b = fc + random.Next(bc - bc);
            Color rs = Color.FromArgb(r, g, b);
            return rs;
        }

        //取随机产生的认证码(数字)
        public string GetByRndNum(int VcodeNum)
        {

            string VNum = "";

            Random rand = new Random();

            for (int i = 0; i < VcodeNum; i++)
            {
                VNum += VcArray[rand.Next(0, 9)];
            }
           
            return VNum;
        }
        private static readonly string[] VcArray = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    }
}
